package com.eightbears.beboy.ui.user.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.SearchClassifyAdapter;
import com.eightbears.beboy.adapter.SearchHistoryAdapter;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.cache.SearchKeyCache;
import com.eightbears.beboy.base.util.InputMethodUtils;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.view.ListItemDecoration;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.base.view.dialog.DefaultDialog;
import com.eightbears.beboy.base.view.layout.MyLinearLayoutManager;
import com.eightbears.beboy.entity.ClassifyEntity;
import com.eightbears.greendao.SearchKey;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.eightbears.beboy.base.util.CommonAPI.SEARCH_KEY_POST_SHOP;


public class SearchShopActivity extends BaseActivity {

    public static final String PARAMS_SEARCH_KEY = "searchKey";
    public static final String PARAMS_SEARCH_SHOP_TYPE = "shopType";

    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;
    @BindView(R.id.rv_shop_classify)
    RecyclerView rv_shop_classify;
    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.et_search_shop)
    EditText et_search_shop;
    @BindView(R.id.view_empty)
    View view_empty;

    private List<SearchKey> list;
    private SearchHistoryAdapter mSearchHistoryAdapter;
    private SearchClassifyAdapter mSearchClassifyAdapter;

    private ClassifyEntity mClassifyEntity;
    private SearchKey mSearchKey;

    private String searchText;
    private int mPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_shop);

        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initAdapter();
    }

    @Override
    protected void initData() {
        list = SearchKeyCache.getInstance(this).getCacheByType(SEARCH_KEY_POST_SHOP);
        mSearchHistoryAdapter.setNewData(list);

        if(list == null || list.size() == 0) {
            view_empty.setVisibility(View.VISIBLE);
            rv_list.setVisibility(View.GONE);
        } else {
            view_empty.setVisibility(View.GONE);
            rv_list.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected void initEvent() {
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                InputMethodUtils.closeInputMethod(SearchShopActivity.this, et_search_shop);
                finish();
            }
        });

        mSearchClassifyAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mClassifyEntity = (ClassifyEntity) adapter.getData().get(position);
                if (view.getId() == R.id.ll_class) {
                    startLocalActivity(SearchShopActivity.this, SearchShopResultActivity.class, PARAMS_SEARCH_SHOP_TYPE, mClassifyEntity.getId());
                }
            }
        });

        mSearchHistoryAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mSearchKey = (SearchKey) adapter.getData().get(position);
                mPosition = position;
                if(view.getId() == R.id.tv_search_title) {
                    startLocalActivity(SearchShopActivity.this, SearchShopResultActivity.class, PARAMS_SEARCH_KEY, mSearchKey.getSearch_title());
                }

                if(view.getId() == R.id.ll_del_item) {
                        deleteItem();
                }
            }
        });
    }

    @OnClick(R.id.ll_back)
    void back() {
        InputMethodUtils.closeInputMethod(SearchShopActivity.this, et_search_shop);
        finish();
    }

    private void initAdapter() {
        ListItemDecoration listItemDecoration = new ListItemDecoration(this, 3, R.color.line_color);
        listItemDecoration.setColor(R.color.line_color);
        mSearchClassifyAdapter = new SearchClassifyAdapter();
        rv_shop_classify.addItemDecoration(listItemDecoration);
        rv_shop_classify.setLayoutManager(new GridLayoutManager(mContext, 3));
        rv_shop_classify.setAdapter(mSearchClassifyAdapter);

        rv_list.setLayoutManager(new MyLinearLayoutManager(this));
        mSearchHistoryAdapter = new SearchHistoryAdapter();
        mSearchHistoryAdapter.openLoadAnimation();
        rv_list.setAdapter(mSearchHistoryAdapter);
    }

    /**
     * 删除item
     */
    void deleteItem() {
        //弹出对话框
        DefaultDialog.Builder defaultDialog = new DefaultDialog.Builder(mContext);
        defaultDialog.setTitle(getString(R.string.alert_text));
        defaultDialog.setMessage(getString(R.string.alert_delete_search_item));

        defaultDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //删除数据库
                SearchKeyCache.getInstance(getApplication()).deleteItemByName(mSearchKey.getSearch_title(), SEARCH_KEY_POST_SHOP);
                mSearchHistoryAdapter.remove(mPosition);
                mSearchHistoryAdapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        });
        defaultDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        defaultDialog.create().show();
    }

    private boolean checkInput() {
        searchText = et_search_shop.getText().toString().trim();
        if (TextUtils.isEmpty(searchText)) {
            ShowToast.showShortToast(R.string.alert_input_empty);
            return false;
        }

        if (searchText.length() > 35) {
            ShowToast.showShortToast(R.string.alert_input_to_long);
            return false;
        }

        if(!isNetWorkConnected(this)){
            return false;
        }

        return true;
    }

    @OnClick(R.id.tv_search)
    void sendSearSearch() {
        if (checkInput()) {
            SearchKey searchKey = new SearchKey(SEARCH_KEY_POST_SHOP, searchText);
            SearchKeyCache.getInstance(this).saveData(searchKey);
            mSearchHistoryAdapter.addData(searchKey);
            mSearchHistoryAdapter.notifyDataSetChanged();
            et_search_shop.setText("");
            view_empty.setVisibility(View.GONE);
            rv_list.setVisibility(View.VISIBLE);
            startLocalActivity(this, SearchShopResultActivity.class, PARAMS_SEARCH_KEY, searchText);
        }
    }

    /**
     * 删除
     */
    @OnClick(R.id.iv_delete)
    void deleteAll() {
        if(mSearchHistoryAdapter == list || mSearchHistoryAdapter.getData().size() < 0) {
            ShowToast.showShortToast(getString(R.string.alert_search_list_empty));
            return;
        }
        DefaultDialog.Builder defaultDialog = new DefaultDialog.Builder(this);
        defaultDialog.setTitle(R.string.alert_text);
        defaultDialog.setMessage(getString(R.string.alert_delete_search_item));
        defaultDialog.setPositiveButton(R.string.text_agree, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SearchKeyCache.getInstance(mContext).clearAllData();
                mSearchHistoryAdapter.getData().clear();
                mSearchHistoryAdapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        });
        defaultDialog.setNegativeButton(R.string.text_cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        defaultDialog.create().show();
    }
}